package com.rr.mymoney.dao.impl.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.rr.mymoney.dao.TagDao;
import com.rr.mymoney.model.Tag;

@Repository
public class TagDaoImpl extends AbstractDao implements TagDao {
    public void saveOrUpdate(Tag tag) {
	getCurrentSession().saveOrUpdate(tag);
	getCurrentSession().flush();
    }

    public Tag get(Long id) {
	return (Tag) getCurrentSession().get(Tag.class, id);
    }

    public Tag getByName(String tagName) {
	@SuppressWarnings("unchecked")
	List<Tag> list = find("from Tag where name = ?", tagName);
	if (list.size() > 0) {
	    return list.get(0);
	} else {
	    return null;
	}
    }

    public void delete(Tag tag) {
	getCurrentSession().delete(tag);
    }
}
